package com.hy.dao.impl;
/*
 * @ Date: 2024/5/14 22:29
 * @ Description: BuytableDaoImpl
 **/

import com.hy.bean.Buytable;
import com.hy.dao.BuytableDao;
import com.hy.exception.BizException;
import com.hy.utils.DBManager;
import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.GenerousBeanProcessor;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.Connection;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

public class BuytableDaoImpl implements BuytableDao {
    QueryRunner qr = new QueryRunner();
    private static BasicRowProcessor basicRowProcessor = new BasicRowProcessor(new GenerousBeanProcessor());

    @Override
    public Buytable queryById(Integer id) {
        return null;
    }

    @Override
    public List<Map<String,Object>> queryAllByLimit(Integer page, Integer limit) throws Exception {
        Connection conn = DBManager.getConnection();
        String sql = "select b.*,bp.totalPrice from buytable b inner join buy_parttable bp on b.id = bp.buytable_id order by b.id limit ?,?";
        List<Map<String, Object>> query = qr.query(conn, sql, new MapListHandler(),page-1,limit);
        return query;
    }

    @Override
    public long count() throws Exception {
        Connection conn = DBManager.getConnection();
        String sql = "select count(1) from buytable b inner join buy_parttable bp on b.id = bp.buytable_id";
        Long query = qr.query(conn, sql, new ScalarHandler<Long>());
        return query;
    }

    @Override
    public long countDetails(Integer id) throws Exception {
        Connection conn = DBManager.getConnection();
        String sql = "select count(1) from buytable b \n" +
                "left join buy_parttable bp on b.id = bp.buytable_id \n" +
                "left join carpartstable c on c.id= bp.parttable_id\n" +
                "inner join producertable p on p.id = c.producertable_id\n" +
                "where b.id = ?";
        Long query = qr.query(conn, sql, new ScalarHandler<Long>(),id);
        return query;
    }

    @Override
    public int insert(Buytable buytable) {
        return 0;
    }

    @Override
    public int update(Buytable buytable) {
        return 0;
    }

    @Override
    public int deleteById(Integer id) {
        return 0;
    }


    @Override
    public List<Map<String, Object>> queryAllDetails(Integer id, Integer page, Integer limit) throws Exception {
        Connection conn = DBManager.getConnection();
        String sql = "select c.id,requestbuyer,requesttime,partcode,partname,partplacecode,\n" +
                "partunit,producername,buycount from buytable b \n" +
                "left join buy_parttable bp on b.id = bp.buytable_id \n" +
                "left join carpartstable c on c.id= bp.parttable_id\n" +
                "inner join producertable p on p.id = c.producertable_id\n" +
                "where b.id = ?\n" +
                "limit ?,?";
        List<Map<String, Object>> query = qr.query(conn, sql, new MapListHandler(),id,page-1,limit);
        return query;
    }

    public List<Buytable> selectBuyTableState(String state) {
        List<Buytable> buytableList = null;
        try {
            Connection conn = DBManager.getConnection();

            String sql = "select * from buytable where state = ?";

            buytableList = qr.query(conn,sql,new BeanListHandler<>(Buytable.class,basicRowProcessor),state);

            DbUtils.close(conn);
        } catch (Exception e) {
            throw new BizException("数据库连接异常",e);
        }

        return buytableList;
    }
}
